package Test03;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author 彪哥
 * @version 1.0
 */
public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        boolean Button = true;
        while (Button) {
            //打印菜单
            System.out.println("--------欢迎来到【老马书城】-------");
            System.out.println("1.展示书籍");
            System.out.println("2.上新书籍");
            System.out.println("3.下架书籍");
            System.out.println("4.退出应用");
            //引入Sannner类
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入您想要执行的功能的序号：");
            int choice = sc.nextInt();    //键盘录入数据后，点击回车，程序才会接收数据

            //根据choice接收的功能序号进行后续判断
            switch (choice) {
                case 1:
                    System.out.println("【老马书城】>>>>>>>>1.展示书籍");

                    //创建File类对象
                    File file = new File("D:\\code\\coding\\test_j\\Test\\src\\Test03\\Demo.txt");

                    if (file.exists() == true) {  // 如果文件存在
                        //创建一个输入流，读取文件中的集合
                        FileInputStream fis = new FileInputStream(file);
                        ObjectInputStream ois = new ObjectInputStream(fis);

                        //读取的集合转换成集合类型
                        ArrayList list = (ArrayList)(ois.readObject());

                        //对集合遍历查看
                        for (int i = 0; i < list.size(); i++) {
                            Book b = (Book)(list.get(i));
                            System.out.println(b.toString());
                        }
                    }else {  //  文件不存在  输出提示
                        System.out.println("当前没有上书籍，请先上新书籍再进行书籍展示。");
                    }
                    break;
                case 2:
                    System.out.println("【老马书城】>>>>>>>>2.上新书籍");
                    Book book = new Book();
                    System.out.println("请输入书籍编号：");
                    book.setBookID(sc.nextInt());
                    System.out.println("请输入书籍名称：");
                    book.setBookName(sc.next());
                    System.out.println("请输入书籍作者：");
                    book.setBookAuthor(sc.next());

                    //封装一个 File类对象
                    File f = new File("D:\\code\\coding\\test_j\\Test\\src\\Test03\\Demo.txt");

                    //对文件进行判断
                    if (f.exists() == true) {  //如果文件存在
                        //创建流 读取文件中的集合
                        FileInputStream fis = new FileInputStream(f);
                        ObjectInputStream ois = new ObjectInputStream(fis);

                        ArrayList list = (ArrayList)(ois.readObject());

                        //把书籍对象添加进集合
                        list.add(book);
                        //创建流  将集合对象写出到硬盘文件中
                        FileOutputStream fos = new FileOutputStream(f);
                        ObjectOutputStream oos = new ObjectOutputStream(fos);

                        //执行写入操作
                        oos.writeObject(list);
                        //关闭流
                        oos.close();
                    }else {  //如果文件不存在，证明是第一次上新书籍
                        //创建一个集合用于存放书籍对象
                        ArrayList arrayList = new ArrayList<Book>();
                        arrayList.add(book);

                        //创建流  将集合对象写出到硬盘文件中
                        FileOutputStream fos = new FileOutputStream(f);
                        ObjectOutputStream oos = new ObjectOutputStream(fos);

                        //执行写入操作
                        oos.writeObject(arrayList);
                        //关闭流
                        oos.close();
                    }
                    break;
                case 3:
                    System.out.println("【老马书城】>>>>>>>>3.下架书籍");

                    //创建File类对象
                    File file1 = new File("D:\\code\\coding\\test_j\\Test\\src\\Test03\\Demo.txt");
                    //对文件是否在存在进行一个判定
                    if (file1.exists() == true) {   //文件存在
                        //创建输入流 读取文件中的集合
                        FileInputStream fis1 = new FileInputStream(file1);
                        ObjectInputStream ois1 = new ObjectInputStream(fis1);

                        ArrayList list1 = (ArrayList)(ois1.readObject());

                        System.out.println("请输入要下架的书籍编号:");
                        int delNo = sc.nextInt();
                        for (int i = 0; i < list1.size(); i++) {
                            Book b = (Book)(list1.get(i));
                            if (delNo == b.getBookID()) {
                                list1.remove(b);
                                //创建输出流
                                FileOutputStream fos1 = new FileOutputStream(file1);
                                ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
                                //写入
                                oos1.writeObject(list1);
                                //关闭流
                                oos1.close();

                                System.out.println("书籍下架成功！");
                                break;
                            }
                        }
                    }else {
                        System.out.println("书城里没有书籍，请先上新书籍。");
                    }
                    break;
                case 4:
                    System.out.println("【老马书城】>>>>>>>>4.退出应用");
                    Button = false;
                    break;
            }


        }
    }


}
